import pygame
import sys

# 初始化pygame
pygame.init()

# 设置游戏窗口
WIDTH, HEIGHT = 800, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("高级双人格斗游戏")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
PURPLE = (128, 0, 128)

# 玩家类
class Fighter:
    def __init__(self, x, y, width, height, color, controls):
        self.rect = pygame.Rect(x, y, width, height)
        self.color = color
        self.vel_y = 0
        self.jumping = False
        self.health = 200
        self.stamina = 100  # 新增耐力值
        self.controls = controls
        self.attacking = False
        self.heavy_attacking = False
        self.attack_cooldown = 0
        self.facing_right = True if color == BLUE else False
        self.attack_triggered = False  # 防止长按连续攻击
        self.heavy_attack_triggered = False
    
    def move(self):
        # 重置水平移动
        dx = 0
        dy = 0
        
        # 获取按键状态
        keys = pygame.key.get_pressed()
        
        # 移动控制
        if keys[self.controls["left"]]:
            dx = -5
            self.facing_right = False
        if keys[self.controls["right"]]:
            dx = 5
            self.facing_right = True
            
        # 跳跃控制 (消耗15耐力)
        if keys[self.controls["up"]] and not self.jumping and self.stamina >= 15:
            if not self.jumping:  # 防止重复触发
                self.vel_y = -15
                self.jumping = True
                self.stamina -= 15
        
        # 普通攻击 (消耗20耐力)
        if keys[self.controls["attack"]] and not self.attack_triggered and self.attack_cooldown == 0 and self.stamina >= 20:
            self.attacking = True
            self.attack_cooldown = 25  # 攻击冷却时间
            self.stamina -= 20
            self.attack_triggered = True
        elif not keys[self.controls["attack"]]:
            self.attack_triggered = False
            
        # 重击 (消耗30耐力)
        if keys[self.controls["heavy_attack"]] and not self.heavy_attack_triggered and self.attack_cooldown == 0 and self.stamina >= 30:
            self.heavy_attacking = True
            self.attack_cooldown = 35  # 重击冷却时间更长
            self.stamina -= 30
            self.heavy_attack_triggered = True
        elif not keys[self.controls["heavy_attack"]]:
            self.heavy_attack_triggered = False
        
        # 应用重力
        self.vel_y += 1
        if self.vel_y > 10:
            self.vel_y = 10
        dy += self.vel_y
        
        # 确保玩家不会离开屏幕
        if self.rect.left + dx < 0:
            dx = -self.rect.left
        if self.rect.right + dx > WIDTH:
            dx = WIDTH - self.rect.right
        if self.rect.bottom + dy > HEIGHT - 50:
            self.jumping = False
            dy = HEIGHT - 50 - self.rect.bottom
            self.vel_y = 0
        
        # 更新位置
        self.rect.x += dx
        self.rect.y += dy
        
        # 攻击冷却
        if self.attack_cooldown > 0:
            self.attack_cooldown -= 1
        else:
            self.attacking = False
            self.heavy_attacking = False
        
        # 耐力恢复 (每秒恢复10点)
        self.stamina = min(100, self.stamina + 0.1667)  # 每帧恢复10/60≈0.1667
    
    def take_damage(self, amount):  # 新增的受伤方法
        self.health -= amount
        if self.health < 0:
            self.health = 0
    
    def draw(self, surface):
        pygame.draw.rect(surface, self.color, self.rect)
        
        # 绘制血条
        pygame.draw.rect(surface, RED, (self.rect.x, self.rect.y - 40, self.rect.width, 10))
        pygame.draw.rect(surface, GREEN, (self.rect.x, self.rect.y - 40, self.rect.width * (self.health / 200), 10))
        
        # 绘制耐力条
        pygame.draw.rect(surface, PURPLE, (self.rect.x, self.rect.y - 25, self.rect.width, 10))
        pygame.draw.rect(surface, YELLOW, (self.rect.x, self.rect.y - 25, self.rect.width * (self.stamina / 100), 10))
        
        # 绘制攻击范围 (比原来缩小)
        if self.attacking or self.heavy_attacking:
            attack_width = 30  # 攻击范围缩小为30
            if self.facing_right:
                attack_rect = pygame.Rect(self.rect.right, self.rect.y, attack_width, self.rect.height)
            else:
                attack_rect = pygame.Rect(self.rect.left - attack_width, self.rect.y, attack_width, self.rect.height)
            
            # 重击显示橙色，普通攻击显示黄色
            attack_color = ORANGE if self.heavy_attacking else YELLOW
            pygame.draw.rect(surface, attack_color, attack_rect, 2)
            return attack_rect, self.heavy_attacking
        return None, False

# 创建玩家
player1 = Fighter(100, HEIGHT - 150, 50, 100, BLUE, {
    "left": pygame.K_a,
    "right": pygame.K_d,
    "up": pygame.K_w,
    "attack": pygame.K_j,      # 普通攻击
    "heavy_attack": pygame.K_k # 重击
})

player2 = Fighter(650, HEIGHT - 150, 50, 100, RED, {
    "left": pygame.K_LEFT,
    "right": pygame.K_RIGHT,
    "up": pygame.K_UP,
    "attack": pygame.K_KP1,    # 小键盘1普通攻击
    "heavy_attack": pygame.K_KP2 # 小键盘2重击
})

# 游戏地面
ground = pygame.Rect(0, HEIGHT - 50, WIDTH, 50)

# 游戏主循环
clock = pygame.time.Clock()

# 字体设置
try:
    font = pygame.font.SysFont("SimHei", 24)
except:
    try:
        font = pygame.font.Font("simhei.ttf", 24)
    except:
        font = pygame.font.Font(None, 24)

def draw_winner(text):
    winner_text = font.render(text, True, WHITE)
    screen.blit(winner_text, (WIDTH//2 - winner_text.get_width()//2, HEIGHT//2 - winner_text.get_height()//2))
    pygame.display.update()
    pygame.time.delay(3000)

running = True
while running:
    clock.tick(60)
    screen.fill(BLACK)
    
    # 绘制地面
    pygame.draw.rect(screen, (100, 100, 100), ground)
    
    # 移动玩家
    player1.move()
    player2.move()
    
    # 绘制玩家并获取攻击信息
    attack1, is_heavy1 = player1.draw(screen)
    attack2, is_heavy2 = player2.draw(screen)
    
    # 检测攻击
    if attack1 and attack1.colliderect(player2.rect):
        damage = 3 if is_heavy1 else 2  # 重击伤害3(1.5倍)，普通攻击2
        player2.take_damage(damage)
        
    if attack2 and attack2.colliderect(player1.rect):
        damage = 3 if is_heavy2 else 2  # 重击伤害3(1.5倍)，普通攻击2
        player1.take_damage(damage)
    
    # 检查游戏结束条件
    if player1.health <= 0:
        draw_winner("玩家2获胜!")
        running = False
    elif player2.health <= 0:
        draw_winner("玩家1获胜!")
        running = False
    
    # 显示控制说明
    controls_text = [
        "玩家1: WASD移动, J攻击(20耐力), K重击(30耐力), W跳跃(15耐力)",
        "玩家2: 方向键移动, 小键盘1攻击(20耐力), 小键盘2重击(30耐力), 上箭头跳跃(15耐力)"
    ]
    
    for i, text in enumerate(controls_text):
        text_surface = font.render(text, True, WHITE)
        screen.blit(text_surface, (WIDTH//2 - text_surface.get_width()//2, 10 + i * 30))
    
    # 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    pygame.display.update()

pygame.quit()
sys.exit()
